package com.cg.controller;

import com.cg.dto.*;
import com.cg.entity.Role;
import com.cg.service.RoleService;
import com.cg.vo.RoleVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Role)表控制层
 *
 * @author chengong
 * @since 2023-12-10 09:56:33
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    @PostMapping("/queryRoleByRoleName")
    public Result<Map<String,Object>> queryRoleByRoleName(@RequestBody RoleDto roleDto){
        PageInfo<RoleVo> roleVoPageInfo = roleService.queryRoleByRoleName(roleDto);
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("pageInfo",roleVoPageInfo);
        return ResultUtils.returnDataSuccess(objectMap);
    }


    /**
     * 角色授权
     * @param roleSourceDto
     * @return
     */
    @PostMapping("/createRoleSource")
    public Result createRoleSource(@RequestBody RoleSourceDto roleSourceDto){
        roleService.createRoleSource(roleSourceDto);
        return ResultUtils.returnSuccess();
    }

    /**
     * 验证角色编码
     * @param roleCode
     * @return
     */
    @GetMapping("/checkRoleCode")
    public Result checkRoleCode(@RequestParam("roleCode") String roleCode){
        Integer integer = roleService.checkRoleCode(roleCode);
        if(integer >0){
            return ResultUtils.returnResult(ResultEnum.FAIL_ROLE_CODE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 创建角色
     */
    @PostMapping("/insertRole")
    public Result insertRole(@RequestBody Role role){
        Integer integer = roleService.insertRole(role);
        if(integer == 0){
            return ResultUtils.returnResult(ResultEnum.FAIL_OPERATE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 角色信息修改
     * @param role
     * @return
     */
    @PostMapping("/setRole")
    public Result updateRole(@RequestBody Role role){
        Integer integer = roleService.updateRole(role);
        if(integer == 0){
            return ResultUtils.returnResult(ResultEnum.COMMON_EXCEPTION);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 角色信息删除
     * @param roleId
     * @return
     */
    @PostMapping("/removeRole")
    public Result deleteRoleByRoleId(@RequestParam("roleId")Integer roleId){
        return roleService.deleteRoleByRoleId(roleId);
    }

}

